//
//  HLoginViewController.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/9.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HLoginViewController.h"
#import "WCustomNavigationController.h"
#import "ViewController.h"
#import "HHomeViewController.h"
#import "UIImage+Color.h"
#import "UtilsMacro.h"
#import "ViewController.h"
#import <AFNetworking.h>
#import "NetworkManager.h"
#import "MBProgressHUD.h"
#import "Networking.h"
#import "HCommon.h"
#import "HAPIManager.h"
#import <TMCache.h>
#import "HLogin.h"
#import "AppDelegate.h"
#import "UserDefault.h"
#import "HOwner.h"
#import <CommonCrypto/CommonHMAC.h>
#import<CommonCrypto/CommonCryptor.h>
@interface HLoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField *text;
@property (nonatomic,strong)UILabel *label;
@property (nonatomic,strong)UIButton *button;
@property (nonatomic,strong)UIImageView *image;
@property (nonatomic,strong)UIButton *phoneButton;
@property (nonatomic,strong)NSData * date;
@property (nonatomic,copy)NSString *datastring;
@property (nonatomic,strong)UITextField *user;
@property (nonatomic,strong)UIImageView *userImage;
@property (nonatomic,strong)UIImageView *passImage;
@property (nonatomic,strong)UITextField *pass;
@property (nonatomic,strong)UIButton *FindPassword;
@property (nonatomic,strong)UIView *DownViewUser;
@property (nonatomic,strong)UIView *DownViewPassWord;
@property (nonatomic,assign)NSNumber *count;
@property (nonatomic,copy)NSString *inbox;
@property (nonatomic ,assign)NSInteger totalInBox;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic, strong)HOwner *model;

@end

@implementation HLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.image];
    [self.view addSubview:self.button];
    [self.view addSubview:self.label];
    [self.view addSubview:self.phoneButton];
    [self.view addSubview:self.FindPassword];
    [self.view addSubview:self.DownViewUser];
    [self.view addSubview:self.DownViewPassWord];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = RGB(13, 140, 236);
    //点击空白处回收键盘
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];


}
- (UIView *)DownViewUser
{
    if (!_DownViewUser) {
        _DownViewUser = [[UIView alloc]initWithFrame:CGRectMake(20, 250, ScreenWidth - 40, 50)];
        _DownViewUser.layer.borderWidth = 1;
        _DownViewUser.layer.borderColor = [RGB(218, 218, 218)CGColor];
        _DownViewUser.layer.cornerRadius = 10;
        _DownViewUser.layer.masksToBounds = YES;
        [_DownViewUser addSubview:self.userImage];
        [_DownViewUser addSubview:self.user];
    }
    return _DownViewUser;
}
- (UIView *)DownViewPassWord
{
    if (!_DownViewPassWord) {
        _DownViewPassWord = [[UIView alloc]initWithFrame:CGRectMake(20, 302, ScreenWidth - 40, 50)];
        _DownViewPassWord.layer.borderWidth =1;
        _DownViewPassWord.layer.borderColor = [RGB(218, 218, 218)CGColor];
        [_DownViewPassWord addSubview:self.pass];
        _DownViewPassWord.layer.cornerRadius = 10;
        _DownViewPassWord.layer.masksToBounds = YES;
        [_DownViewPassWord addSubview:self.passImage];
    }
    return _DownViewPassWord;
}
- (UIImageView *)image {
    if (!_image) {
        _image = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth -111)/2, 100,111 , 111)];
        _image.image = [UIImage imageNamed:@"icon"];
        
    }
    return _image;
}
- (UIImageView *)userImage
{
    if (!_userImage) {
        _userImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 15, 15,15 )];
        _userImage.image = [UIImage imageNamed:@"icon-touxaing2"];
    }
    return _userImage;
}
- (UIImageView *)passImage
{
    if (!_passImage) {
        _passImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 15, 20, 20)];
        _passImage.image = [UIImage imageNamed:@"密码 (1)"];
    }
    return _passImage;
}
- (UITextField *)user
{
    if (!_user) {
        _user = [[UITextField alloc]initWithFrame:CGRectMake(28, 0, self.DownViewUser.width - 23, self.DownViewUser.height)];
        _user.delegate = self;
        _user.borderStyle  = UITextBorderStyleNone;
        _user.placeholder = @"请输入您的账号";
    }
    return _user;
}
- (UITextField *)pass
{
    if (!_pass) {
        _pass = [[UITextField alloc]initWithFrame:CGRectMake(28, 0, self.DownViewPassWord.width - 23, self.DownViewPassWord.height)];
        _pass.delegate = self;
        _pass.borderStyle = UITextBorderStyleNone;
        _pass.placeholder = @"请输入您的密码";
        _pass.secureTextEntry = YES;
    }
    return _pass;
}

- (UIButton *)FindPassword
{
    if (!_FindPassword) {
        _FindPassword = [UIButton buttonWithType:UIButtonTypeCustom];
        _FindPassword.frame = CGRectMake(ScreenWidth - 100, self.button.bottom+10, 70, 20);
        _FindPassword.titleLabel.font = [UIFont systemFontOfSize:14];
        [_FindPassword setTitle:@"登录设置" forState:UIControlStateNormal];
        [_FindPassword setTitleColor:RGB(13, 140, 236) forState:UIControlStateNormal];
        [_FindPassword addTarget:self action:@selector(findPassWord:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _FindPassword;
}
- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(20, 360, ScreenWidth-40, 40);
        _button.backgroundColor = RGB(13, 140, 236);
        _button.layer.masksToBounds = YES;
        _button.layer.cornerRadius = 7;
        [_button setTitle:@"登录" forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _button.titleLabel.font = [UIFont systemFontOfSize:16];
        [_button addTarget:self action:@selector(handleclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _button;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // 回收键盘
    [textField resignFirstResponder];
    return YES;
    
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc]init];
        _label.frame = CGRectMake(ScreenWidth *0.23, ScreenHeight-50, 100, 30);
        _label.text =@"技术服务电话:";
        _label.textColor = [UIColor grayColor];
        _label.font = [UIFont systemFontOfSize:15];
    }
    return _label;
}
- (UIButton *)phoneButton {
    if (!_phoneButton) {
        _phoneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _phoneButton.frame = CGRectMake(self.label.right +5, ScreenHeight-50, ScreenWidth *0.5, 30);
        [_phoneButton setTitle:@"0571-89932321" forState:UIControlStateNormal];
        [_phoneButton setTitleColor:RGB(13, 140, 236) forState:UIControlStateNormal];
        _phoneButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_phoneButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_phoneButton addTarget:self action:@selector(handcall:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _phoneButton;
}

//拨打技术客服电话
- (void)handcall:(UIButton *)sender {
    
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneButton.titleLabel.text];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
    
    
}

- (void)handleclick:(UIButton *)sender {
    if (self.user.text.length ==0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"系统提示" message:@"请输入正确账号" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
        }]];
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else {
        NSString *PassWord = [[NetworkManager md5:self.pass.text] lowercaseString]?:@"";
        if (self.pass.text.length == 0) {
            PassWord = @"";
        }
        [HCommon popupMessage:@"登录中..."];
        [[[[HLogin sharedInstance]loginWithMobileNumber:self.user.text password:PassWord]doError:^(NSError *error) {
            [HCommon popupErrorMessage:error.userInfo[@"message"]];
            
        }]subscribeNext:^(id x) {
            [HCommon popupSuccessMessage:@"登录成功"];
        }];
        [NetworkManager md5:self.pass.text];
        NSString *url = [NSString stringWithFormat:@"%@/login?user_code=%@&user_password=%@",Hurl,self.user.text,PassWord];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
        manager.responseSerializer = [AFJSONResponseSerializer serializer];
        [manager GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            NSDictionary *dataDic = [responseObject objectForKey:@"data"];
            [[TMCache sharedCache]setObject:[[responseObject objectForKey:@"menu"]allValues] forKey:@"menu"];
            [[TMCache sharedCache]setObject:[dataDic objectForKey:@"EMP_ID"]forKey:@"emp_id"];
            HOwner *model = [[HOwner alloc] init];
            NSDictionary *data = [NSDictionary dictionaryWithDictionary:[responseObject objectForKey:@"data"]];
            [model setValuesForKeysWithDictionary:data];
            // 将登录成功数据转入
//            [UserDefault saveUserInfo:model];
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
        
    }
}

- (void)findPassWord:(UIButton *)sender
{
}

- (void)keyboardHide:(UITapGestureRecognizer *)tap {
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
